package com.estar.assistsurvy.activity;

import java.util.ArrayList;
import java.util.List;

import org.litepal.crud.DataSupport;

import com.estar.assistsurvey.entity.ImageVO;
import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.PhotoviewAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
//import android.widget.RelativeLayout;

public class PhotoDetailActivity extends BaseActivity implements OnClickListener{
	private ViewPager photo_vp;
	private ImageView prePage,nextPage;
	private int index=0;
	private String rptNo;
	private Intent intent;
	private List<ImageVO> list;
	private PhotoviewAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photodetail);
		init();
	}
	
	private void init(){
		photo_vp=find(R.id.photo_vp);
		nextPage=find(R.id.nextPage);
		nextPage.setOnClickListener(this);
		prePage=find(R.id.prePage);
		prePage.setOnClickListener(this);
		intent=getIntent();
		rptNo=intent.getStringExtra("rptNo");
		index=intent.getIntExtra("position", 0);
		list=new ArrayList<>();
		if(DataSupport.where("rptNo=?",rptNo).find(ImageVO.class)!=null){
			list=DataSupport.where("rptNo=?",rptNo).find(ImageVO.class);
		}else{
			showShortToast("当前案件没有拍照!");
		}
		adapter=new PhotoviewAdapter(list, this);
		photo_vp.setAdapter(adapter);
		photo_vp.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		photo_vp.setCurrentItem(index);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.prePage:
			if(index>=0){
				index--;
				photo_vp.setCurrentItem(index);
			}else{
				showShortToast("当前已是第一张!");
			}
			break;
			
		case R.id.nextPage:
			if(index<list.size()){
				index++;
				photo_vp.setCurrentItem(index);
			}else{
				showShortToast("当前已是最后一张!");
			}
			break;

		default:
			break;
		}
	}
}
